package main

import (
	"fmt"
	"runtime"
	"sync"
	"time"
)

// 定义一个协程计数器
var wg sync.WaitGroup

func test1() {
	for i := 0; i < 100; i++ {
		fmt.Println("test1,你好golang")
		//time.Sleep(time.Millisecond * 100)
	}
	// 协程计数器减1
	wg.Done()
}

func test2() {
	for i := 0; i < 100; i++ {
		fmt.Println("test2,你好golang")
		//time.Sleep(time.Millisecond * 100)
	}
	// 协程计数器减1
	wg.Done()
}

func main() {

	nmpCpu := runtime.NumCPU()
	fmt.Println("cpu个数为:", nmpCpu)
	runtime.GOMAXPROCS(runtime.NumCPU() - 1)

	// 通过go关键字开启一个协程
	wg.Add(1)
	go test1()

	// 协程计数器加1
	wg.Add(1)
	go test2()

	for i := 0; i < 100; i++ {
		fmt.Println("main,你好golang")
		time.Sleep(time.Millisecond * 100)
	}

	wg.Wait()
	fmt.Println("主线程退出")
}
